/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugDict;
import com.jims.master.modules.drug.entity.DrugNameDict;
import com.jims.master.modules.drug.service.DrugDictService;
import com.jims.master.modules.drug.service.DrugNameDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 药品字典维护Controller
 * @author zhuq
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugDict")
public class DrugDictController extends BaseController {

    @Autowired
    private DrugDictService drugDictService;
    @Autowired
    DrugNameDictService drugNameDictService;

    @ModelAttribute
    public DrugDict get(@RequestParam(required=false) String id) {
        DrugDict entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = drugDictService.get(id);
        }
        if (entity == null){
            entity = new DrugDict();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(DrugDict drugDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugDict> page = drugDictService.findPage(new Page<DrugDict>(request, response), drugDict);
        model.addAttribute("page", page);
        return "modules/drug/drugDictIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String findlist(DrugDict drugDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugDict> page = drugDictService.findPage(new Page<DrugDict>(request, response), drugDict);
        model.addAttribute("page", page);
        return "modules/drug/drugDictQuery";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"queryList", ""})
    public String findDrugCode(DrugDict drugDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<DrugDict> drugDictList = drugDictService.findByDrugCode(drugDict);
        model.addAttribute("drugDictList", drugDictList);
        model.addAttribute("drugDict", drugDict);
        return "modules/drug/drugDictQuery";
    }

    /**
     * 根据药品drugCode查询列表
     * @param drugDict
     * @author  zhuq
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"dialog", ""})
    public String findByDrugCode(DrugDict drugDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<DrugDict> drugDictList = drugDictService.findByDrugCode(drugDict);
        model.addAttribute("drugDictList", drugDictList);
        model.addAttribute("drugDict", drugDict);
        return "modules/drug/drugDictIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(DrugDict drugDict, Model model) {
        //根据drugCode获取drugName
        DrugNameDict drugNameDict=new DrugNameDict();
        drugNameDict=drugNameDictService.findByDrugCode(drugDict.getDrugCode());
        drugDict.setDrugName(drugNameDict.getDrugName());
        model.addAttribute("drugDict", drugDict);
        return "modules/drug/drugDictForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(DrugDict drugDict, Model model) {
        StringData data=new StringData();
        drugDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(drugDict.getDrugName()));
        int i=drugDictService.save(drugDict);
        int j=drugNameDictService.updateDrug(drugDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存药品成功");
        }else{
            data.setCode("error");
            data.setData("保存药品失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(DrugDict drugDict) {
        StringData data=new StringData();
        int i=drugDictService.delete(drugDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除药品成功");
        }else{
            data.setCode("error");
            data.setData("删除药品失败");
        }
        return data;
    }


    /**
     * @param drugDict 传递参数
     * @return List<DrugDict>    返回类型
     * @throws
     * @Title: autoComplete
     * @Desciption: (药品基本信息添加药品字典自动补全)
     * @author CTQ
     * @date 2017-03-02
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<DrugDict> autoComplete(String q,String limit,DrugDict drugDict) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<DrugDict> list = drugDictService.findAutoCompleteList(drugDict,q,limit);
        return list;
    }

}
